package com.ll.mybatis;

import com.ll.mybatis.dao.DepartmentMapper;
import com.ll.mybatis.dao.EmployeeMapper;
import com.ll.mybatis.dao.EmployeeMapperPlus;
import com.ll.mybatis.entity.Department;
import com.ll.mybatis.entity.Employee;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘龙
 * @version 1.0
 */
public class MybatisXMLCRUDepartment {
    public SqlSessionFactory getsqlSessionFactory() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        return new SqlSessionFactoryBuilder().build(inputStream);
    }

    /**
     * 查询单个部门信息
     */
    @Test
    public void getDepartmentInfo() throws IOException {
        SqlSession sqlSession = getsqlSessionFactory().openSession();
        try {
            DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
            Department department = mapper.getDeptById(1);
            System.out.println(department);
        } finally {
            sqlSession.close();
        }

    }

    /**
     * 查询部门的时候将部门对应的所有员工信息,查询出来
     *  public Department getDeptByIdPlus(@Param("departmentId") Integer departmentId);
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {
        SqlSession sqlSession = getsqlSessionFactory().openSession();

        try {
            DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
            Department department = mapper.getDeptByIdPlus(1);
            List<Employee> employeeList = department.getEmployeeList();
            for (Employee employee : employeeList) {
                System.out.println(department.getDepartmentName() + employee);
            }

        } finally {
            sqlSession.close();
        }
    }


    /**
     *     private Integer departmentId;
     *     private String departmentName;
     *     private List<Employee> employeeList;
     * 查询部门的时候将部门对应的所有员工信息,查询出来,采用分步查询
     * public Department getDeptByIdStep(@Param("departmentId") Integer departmentId);
     * @throws IOException
     */
    @Test
    public void test5() throws IOException {
        SqlSession sqlSession = getsqlSessionFactory().openSession();

        try {
            DepartmentMapper mapper = sqlSession.getMapper(DepartmentMapper.class);
            Department department = mapper.getDeptByIdStep(1);
            List<Employee> employeeList = department.getEmployeeList();
            for (Employee employee : employeeList) {
                System.out.println(department.getDepartmentName() +"=>" + employee);
            }

        } finally {
            sqlSession.close();
        }
    }
}
